package com.ebuy.shopping.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebuy.shopping.common.utils.Contants;
import com.ebuy.shopping.model.Category;
import com.ebuy.shopping.service.ICategoryService;

/**
 * 
 * @ClassName: CategoryController
 * @Description: TODO
 * @author wenzhao
 * @date 2017年8月11日 下午1:14:31
 * 
 */
@Controller
@RequestMapping("/category")
public class CategoryController
{
    @Autowired
    private ICategoryService categoryService;

    @RequestMapping(value = "{id}/show", method = RequestMethod.GET)
    private String show(@PathVariable
    int id, Model model) throws Exception
    {
        Category category = categoryService.load(id);
        model.addAttribute(category);
        return "category/show";

    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) throws Exception
    {
        // List<Category> categories = categoryService.getCategories();
        //
        // model.addAttribute("categories", categories);
        List<Category> gradeList = categoryService
                .getCategoriesByGrade(Contants.GRADE_ONE);
        Map<Category, List<Category>> catMap = new HashMap<Category, List<Category>>();
        for (Category cat : gradeList)
        {
            int catId = cat.getId();
            List<Category> catList = categoryService
                    .getCategoriesByParentId(catId);
            catMap.put(cat, catList);

        }
        model.addAttribute("catMap", catMap);
        return "category/category_list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) throws Exception
    {

        List<Category> gradeList = categoryService
                .getCategoriesByGrade(Contants.GRADE_ONE);
        model.addAttribute("gradeList", gradeList);
        return "category/category_add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Category category) throws Exception
    {
        if (category.getParentId() == 0)
        {
            category.setGrade(Contants.GRADE_ONE);
        }
        else
        {
            category.setGrade(Contants.GRADE_TWO);
        }
        category.setShowIndex(Contants.INDEX_SHOW_TRUE);
        int flag = categoryService.add(category);
        if (flag > 0)
        {
            System.out.println("添加成功！");
        }
        else
        {
            System.out.println("添加失败！");
        }
        return "redirect:/category/add";
    }

    /*
     * 到修改页面
     */
    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String update(@PathVariable
    int id, Model model) throws Exception
    {
        List<Category> gradeList = categoryService
                .getCategoriesByGrade(Contants.GRADE_ONE);
        model.addAttribute("gradeList", gradeList);
        Category cat = categoryService.load(id);
        model.addAttribute("cat", cat);

        return "category/category_update";
    }

    /*
     * 执行修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Model model, Category cat) throws Exception
    {
        categoryService.update(cat);
        return "redirect:/category/list";
    }

    /*
     * 到修改页面
     */
    @RequestMapping(value = "{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable
    int id, Model model) throws Exception
    {

        categoryService.delete(id);

        return "redirect:/category/list";
    }

}
